<template>
  <HeadSix :active-index="activeIndex"></HeadSix>
  <el-row>
    <el-col style="margin-top: 40px; margin-bottom: 50px;">
      <el-steps :space="600" :active="active" finish-status="success" :align-center="true" space="100%">
        <el-step title="存证信息上传" description="上传存证信息"/>
        <el-step title="申请上链" description="申请上链中，是否决定上链"/>
        <el-step title="信息上链成功" description="信息已上链，无法篡改！"/>
      </el-steps>
    </el-col>
  </el-row>
  <el-row :gutter="20">
    <el-col :span="12" >
      <el-card style="margin: 0 0 20px 60px;">
        <enterprise-info-form
            :form="form"
            :disabled="true"
        ></enterprise-info-form>
      </el-card>
    </el-col>
<!--    <el-col :span="6">
      <el-image  v-if="form.value && form.value.businessLicense" style="width: 300px; height: 200px;" :src="form.value.businessLicense">
      </el-image>
      <el-image style="width: 300px; height: 250px; padding-top: 50px;" src="http://ahteaapi.wtycms.cn/Uploads/enterprise/340104003001/honor/be77dd0e131bd6878ce2342b15189fb8de9c3f47a4e555a0421aebe1da644fb0.png">
      </el-image>
    </el-col>
    <el-col :span="6">
      <div>
        <p style="color: #409eff">上链凭证：该信息未上链</p>
        <my-upload
            :file-type="'qyInfo'"
            @get-form-pic-value="getFormPicValue"
        >
        </my-upload>
      </div>
      <div style="padding-top: 60px;">
        <p style="color: #409eff">上链凭证：该信息未上链</p>
        <my-upload
            :file-type="'qyInfo'"
            @get-form-pic-value="getFormPicValue"
        >
        </my-upload>
      </div>
    </el-col>-->
    <el-col :span="3">
        <el-button
            size="large"
            :icon="Connection"
            type="primary"
            @click="dialogVisible = true"
            :disabled="disable"
        >
          存证信息上传
        </el-button>
        <dialog-cer-evi
            v-model="dialogVisible"
            v-if="dialogVisible"
            :option-info="upQyLinkInfo"
            :dialog-cer-evi-title="'存证信息'"
            :dialog-cer-evi-table-value="form"
            @handleConfirm="handleConfirm"
        ></dialog-cer-evi>
    </el-col>
    <el-col :span="3">
      <el-button
          size="large"
          :icon="View"
          color="#626aef"
          @click="drawerVisible = true"
      >
        查看存证信息
      </el-button>
    </el-col>
    <el-col :span="3">
        <el-button
            size="large"
            type="success"
            :icon="Link"
            @click="applyLink"
            :disabled="disable"
        >
          申请上链
        </el-button>
    </el-col>
    <el-col :span="3">
        <el-button
            size="large"
            type="success"
            :icon="Link"
            @click="upLink"
            :disabled="disable"
        >
          确认上链
        </el-button>
    </el-col>
  </el-row>
  <drawer-info
      v-model="drawerVisible"
      :upLinkInfo="form"
      :option-info="qyLinkInfo"
      :title="'企业上链信息'"
  >
  </drawer-info>
</template>

<script setup>
import HeadSix from "@/components/header/headSix.vue";
import EnterpriseInfoForm from "@/components/form/enterpriseInfoForm.vue";
import {computed, onMounted, ref} from "vue";
import {entInfoManage, updateInfo, updateUserInfo} from "@/api/apiPath/onePartApi.js";
import {confirmLink, getOptionInfo, initFormInfo} from "@/api/methods/initInfo.js";
import DrawerInfo from "@/components/dialog/six/drawerInfo.vue";
import {qyLinkInfo} from "@/api/options/sixPart/qyLinkInfo.js";
import {Connection, Link, View} from "@element-plus/icons-vue";
import {ElMessage, ElMessageBox, ElNotification} from "element-plus";
import {
  applyEntCerInfo,
  applyProCerInfo,
  confirmEntCerInfo,
  confirmProCerInfo,
  modifyProCerInfo,
  upProEviInfo
} from "@/api/apiPath/sixPart.js";
import {useUserStore} from "@/stores/myStore.js";
import DialogCerEvi from "@/components/dialog/six/dialogCerEvi.vue";
import {addAndGetData, addTableInfo} from "@/api/methods/addInfo.js";
import {updataInfo, updateEnterpriseForm, updateFormInfo} from "@/api/methods/updateInfo.js";
import {upLinkInfo} from "@/api/options/sixPart/upLinkInfo.js";
import {upQyLinkInfo} from "@/api/options/sixPart/upQyLinkInfo.js";
const drawerVisible = ref(false)
const activeIndex = '/czsystem'
const userInfo = useUserStore()
const active = computed(()=> userInfo.auth.user.active)
const dialogVisible = ref(false)
const disable = computed(()=> userInfo.auth.user.active === 3)
const form = ref({
  qydm: '',
  qymc: '',
  fzr: '',
  fzrtel: '',
  xydm: '',
  addr: '',
  zyyw: '',
  zycp: '',
  jygm: '',
  dwjj: '',
  businessLicense: '',
  cardX: '',
  cardY: '',
  qtpic: '',
  evidence: ''
})
onMounted(async ()=> {
  form.value = await getOptionInfo(entInfoManage())
  console.log(form.value.businessLicense)
  form.value.evidence = userInfo.auth.user.evidence
})
const getFormPicValue = (value)=> {
  console.log(value)
  form.value.businessLicense = value.pic
}
const handleConfirm = async (formValue, formRef)=> {
  form.value = formValue
  formRef.validate( async valid => {
    if (!valid) {
      ElMessage({
        message: "请填写完整！",
        type: 'warning',
        duration: 1500
      })
    } else {
      try {
        updateEnterpriseForm(updateInfo(), form, entInfoManage())
        userInfo.auth.user.active = 1
        await getOptionInfo(updateUserInfo(1))
        dialogVisible.value = false
      }catch (err) {
        console.log("errorOne" + err)
        ElNotification({
          message: "请求失败~",
          type: 'error',
          duration: 1500
        })
      }
    }
  })
}
const applyLink = ()=> {
  ElMessageBox.confirm(
      '是否申请上链？上链前请核对好上链信息！',
      '提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        try {
          if (form.value.evidence === null) {
            userInfo.auth.user.active = 2
            await getOptionInfo(updateUserInfo(2))
            const message = await addAndGetData(applyEntCerInfo(), form)
            ElMessage({
              type: 'success',
              message
            })
          }
        } catch (e) {
          console.log(e)
          ElNotification({
            message: "请求失败",
            type: 'error',
            duration: 1500
          })
        }
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '已取消上链',
        })
      })
}
const upLink = ()=> {
  ElMessageBox.confirm(
      '此操作将上链该信息且上链后无法篡改，是否继续？',
      '提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        try {
          if (form.value.evidence === null) {
            form.value.evidence  = await getOptionInfo(confirmEntCerInfo(userInfo.auth.user.qyIndex))
            userInfo.auth.user.active = 3
            const message = await getOptionInfo(updateUserInfo(3))
            ElMessage({
              type: 'success',
              message
            })
          }
        } catch (e) {
          console.log(e)
          ElNotification({
            message: "请求失败",
            type: 'error',
            duration: 1500
          })
        }
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '已取消申请上链',
        })
      })
}
</script>

<style scoped>

</style>